package com.ruoyi.web.controller.school;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.school.domain.PacAcademicOutput;
import com.ruoyi.school.domain.PacActualOutput;
import com.ruoyi.school.domain.PacProSkillCert;
import com.ruoyi.school.domain.dto.PacProSkillCertListDTO;
import com.ruoyi.school.domain.vo.AcademicCommonVo;
import com.ruoyi.school.service.PacProSkillCertService;
import com.ruoyi.system.domain.TableData;
import com.ruoyi.web.controller.tool.WordUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.ruoyi.web.controller.tool.WordUtil.formatDate;

@RestController
@RequestMapping("/school/pacProSkillCert")
public class PacProSkillCertController extends BaseController {
    @Resource
    private PacProSkillCertService pacProSkillCertService;

    @Resource
    private WordUtil wordUtil;

    @GetMapping("/list")
    public TableDataInfo list(PacProSkillCertListDTO pacProSkillCertListDTO) {
        TableData<PacProSkillCert> list = pacProSkillCertService.getList(pacProSkillCertListDTO);
        return getDataTable2(list.getData(),list.getTotal());
    }

    @GetMapping("/details")
    public AjaxResult details(PacProSkillCert pacProSkillCert) {
        PacProSkillCert details = pacProSkillCertService.getDetails(pacProSkillCert);
        return success(details);
    }

    @Log(title = "技能证书", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PacProSkillCert pacProSkillCert) {
        boolean add = pacProSkillCertService.add(pacProSkillCert);
        return success(add);
    }

    @Log(title = "技能证书", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody PacProSkillCert pacProSkillCert) {
        boolean update = pacProSkillCertService.edit(pacProSkillCert);
        return success(update);
    }

    @Log(title = "技能证书", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult delete(PacProSkillCert pacProSkillCert) {
        boolean delete = pacProSkillCertService.delete(pacProSkillCert);
        return success(delete);
    }

    /**
     * 成果上墙
     */
    @Log(title = "技能证书(上墙)", businessType = BusinessType.UPDATE)
    @PutMapping("/wall")
    public AjaxResult wall(@RequestBody AcademicCommonVo academicCommonVo) {
        boolean wall = pacProSkillCertService.wall(academicCommonVo);
        return success(wall);
    }

    /**
     * 成果下墙
     */
    @Log(title = "技能证书(下墙)", businessType = BusinessType.UPDATE)
    @PutMapping("/unWall")
    public AjaxResult unWall(@RequestBody AcademicCommonVo academicCommonVo) {
        boolean wall = pacProSkillCertService.unWall(academicCommonVo);
        return success(wall);
    }

    /**
     * 获取学生的实战成果
     * @param pacProSkillCert
     * @return
     */
    @GetMapping("/myList")
    public TableDataInfo myList(PacProSkillCert pacProSkillCert) {
        List<PacProSkillCert> list = pacProSkillCertService.myList(pacProSkillCert);
        return getDataTable(list);
    }

    /**
     * 学生新增电子证书
     * @param pacProSkillCert
     * @return
     */
    @Log(title = "技能证书", businessType = BusinessType.INSERT)
    @PostMapping("/addMy")
    public AjaxResult addMy(@RequestBody PacProSkillCert pacProSkillCert) {
        boolean add = pacProSkillCertService.addMy(pacProSkillCert);
        return success(add);
    }

    /**
     * 学生编辑电子证书
     * @param pacProSkillCert
     * @return
     */
    @Log(title = "技能证书", businessType = BusinessType.UPDATE)
    @PutMapping("/updateMy")
    public AjaxResult updateMy(@RequestBody PacProSkillCert pacProSkillCert) {
        boolean update = pacProSkillCertService.updateMy(pacProSkillCert);
        return success(update);
    }

    @PostMapping("/export-word")
    public void exportWord(HttpServletResponse response, PacProSkillCert proSkillCert) throws IOException {
        // 查询详情
        PacProSkillCert details = pacProSkillCertService.getDetails(proSkillCert);
        if (details == null) {
            return;
        }
        // 格式化日期
        String formattedDate = formatDate(details.getCreateTime());
        String titleText = details.getStuName() + "在" + formattedDate + "获得“" + details.getAcTitle() + "”";
        WordUtil.SendWord sendWord = new WordUtil.SendWord();
        sendWord.setTitle(titleText);
        if(!Objects.equals(details.getAcFile(), "") && Objects.equals(details.getAcFileType(), "image")){
            sendWord.setImage(details.getAcFile());
        }
        wordUtil.exportWord(sendWord, response);
    }

    @PostMapping("/export-word2")
    public void exportWord2(HttpServletResponse response, PacProSkillCert proSkillCert) throws IOException {
        List<Long> ids = proSkillCert.getIds();
        if (ids == null || ids.size() == 0) {
            throw new RuntimeException("请选择要导出的数据");
        }
        List<PacProSkillCert> listByIds = new ArrayList<>();
        // 查询详情
        ids.forEach(item -> {
            proSkillCert.setId(item);
            PacProSkillCert details = pacProSkillCertService.getDetails(proSkillCert);
            if (details != null) {
                listByIds.add(details);
            }
        });

        if (listByIds.size() == 0) {
            throw new RuntimeException("请选择要导出的数据");
        }

        List<WordUtil.SendWord> sendWordList = new ArrayList<>();
        listByIds.forEach(details -> {
            // 查询详情
            if (details == null) {
                return;
            }
            // 格式化日期
            String formattedDate = formatDate(details.getCreateTime());
            String titleText = details.getStuName() + "在" + formattedDate + "获得“" + details.getAcTitle() + "”";
            WordUtil.SendWord sendWord = new WordUtil.SendWord();
            sendWord.setTitle(titleText);
            if(!Objects.equals(details.getAcFile(), "") && Objects.equals(details.getAcFileType(), "image")){
                sendWord.setImage(details.getAcFile());
            }
            sendWordList.add(sendWord);
        });

        wordUtil.exportListWord(sendWordList, response);
    }
}
